package com.morty.config.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * ThreadLocal工具类
 */
public class ThreadContextHolder {
    public static final ThreadLocal<Map<String, Object>> context = new MyThreadLocal();

    public static String LOG_TRACE_ID = "traceId";

    private static class MyThreadLocal extends ThreadLocal<Map<String, Object>> {
        @Override
        protected Map<String, Object> initialValue() {
            return new HashMap<String, Object>();
        }
    }

    public static Object get(String key) {
        return getMap().get(key);
    }

    private static Map<String, Object> getMap() {
        return context.get();
    }

    public static void put(String k, Object v) {
        getMap().put(k, v);
    }

    public static void reset() {
        getMap().clear();
    }

    public static void putTraceId(String traceId) {
        getMap().put(LOG_TRACE_ID, traceId);
    }

    public static String getTraceId() {
        return (String)getMap().get(LOG_TRACE_ID);
    }

}
